﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chaos.messages
{
    class request_assign_faction : message
    {

        String m_user;
        public String User
        {
            get { return m_user; }
            set { m_user = value; }
        }

        String m_faction;
        public String Faction
        {
            get { return m_faction; }
            set { m_faction = value; }
        }

        public override void process(context in_context)
        {
            bool assignfaction = true;
            model.game g = System.Windows.Application.Current.FindResource("model_game") as model.game;
            if (g != null)
            {

                model.faction oldfaction = g.findfactionbyuser(User);
                if (oldfaction != null)
                {
                    if (oldfaction.Locked)
                    {
                        assignfaction = false;
                    }
                }

                model.faction newfaction = g.findfactionbyname(Faction);
                if (newfaction != null)
                {
                    if (newfaction.HasUser)
                    {
                        assignfaction = false;
                    }

                    if (newfaction.Locked)
                    {
                        assignfaction = false;
                    }
                }

                if ( !assignfaction )
                {
                    model.appinfo appinfo = System.Windows.Application.Current.FindResource("model_appinfo") as model.appinfo;
                    if (appinfo != null)
                    {
                        if (appinfo.HostName == User)
                        {
                            assignfaction = true;
                        }
                    }
                }
            }

            if (assignfaction)
            {
                AssignFactionEvent e = new AssignFactionEvent();
                e.Faction = Faction;
                e.User = User;
                System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new OnAssignFactionDelegate(OnAssignFactionUIThread), e);
            }
        }

        public override void serialize(stream.stream in_Stream)
        {
            in_Stream.begin("request_assign_faction")
                .serialize("user", ref m_user )
                .serialize("faction", ref m_faction )
            .end();
        }

        public class AssignFactionEvent : EventArgs
        {
            public String Faction { get; set; }
            public String User { get; set; }

            public AssignFactionEvent()
            {
            }
        };
        public delegate void OnAssignFactionDelegate(AssignFactionEvent e);

        public void OnAssignFactionUIThread(AssignFactionEvent e)
        {
            model.game g = System.Windows.Application.Current.FindResource("model_game") as model.game;
            if (g != null)
            {
                model.faction oldfaction = g.findfactionbyuser(User);
                if (oldfaction != null)
                {
                    oldfaction.User = "";
                }
                
                model.faction newfaction = g.findfactionbyname(Faction);
                if (newfaction != null)
                {
                    newfaction.User = User;
                }

                network.gameserver s = System.Windows.Application.Current.FindResource("game_server") as network.gameserver;
                if (s != null)
                {
                    process_model_game message = new process_model_game();
                    message.Game = g;
                    s.BroadcastMessage(message);
                }

            }
        }
    }
}
